#!/bin/bash
# 前提：需要安装好JDK和zookeeper
#提示：考虑到个人开环境云服务器内存较少，可以内存大小(256M或512M就可以，生产环境不建议)
###  执行以下命令：grep 'Xmx' ./bin/*.sh -R 找到对应的文件 修改，重启即可

version="2.8.0"

#获取本机IP
host_addr=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|sed -n '$p'`

# 指定ZK地址
read -p "Enter zookeeper address,default:127.0.0.1:2181 " zkAddress
if [ -z "$zkAddress" ]; then
	zkAddress="127.0.0.1:2181"
fi
echo "your zookeeper address is: $zkAddress"

# 指定外网访问地址
read -p "Enter kafka outer visit address,default:$host_addr:9092 " outerVisitAddress
if [ -z "$outerVisitAddress" ]; then
	outerVisitAddress="$host_addr:9092"
fi
echo "your kafka outer visit is: $outerVisitAddress"


mkdir -p /data/tools
cd /data/tools
echo 'downloading...'
wget -c https://mirrors.huaweicloud.com/apache/kafka/${version}/kafka_2.13-${version}.tgz || exit 1


tar zxvf kafka_2.13-${version}.tgz

ln -s kafka_2.13-${version} kafka

mkdir -p /data/data/kafka
mv /data/tools/kafka/config/server.properties /data/tools/kafka/config/server.properties_bak
cat >/data/tools/kafka/config/server.properties <<EOF
broker.id=0
advertised.listeners=PLAINTEXT://###outerVisitAddress###
zookeeper.connect=###zkAddress###
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/tmp/kafka-logs
num.partitions=1
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connection.timeout.ms=18000
group.initial.rebalance.delay.ms=0
EOF


sed -i 's,###outerVisitAddress###,'$outerVisitAddress',g' /data/tools/kafka/config/server.properties
sed -i 's,###zkAddress###,'$zkAddress',g' /data/tools/kafka/config/server.properties

nohup sh /data/tools/kafka/bin/kafka-server-start.sh /data/tools/kafka/config/server.properties >/dev/null 2>&1 &